package cn.itsource.service.impl;

import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.domain.org.Employee;
import cn.itsource.feign.zuul.LoginInfoFegin;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.service.EmployeeService;
import cn.itsource.vo.EmplQuery;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    LoginInfoFegin userInfoFeignClient;


    @Override
    public PageList<Employee> findPage(EmplQuery baseQuery) {

        return employeeMapper.findPage(baseQuery);
    }

    @Transactional
    @Override
    public AjaxResult insert(Employee employee) {
        //这一步是因为要获取这个还没有进行加密的密码,到时候好发送短信或邮箱给用户
        String passwordReturn = employee.createPasswordReturn();
        employee.setPassword(passwordReturn);
        //初始化employee 信息 包括密码 盐值 以及雪花id
        employee.init();
        employeeMapper.insert(employee);
        //这里是没有加盐加密的密码,是发送给用户手机看的
        employee.setInitPassword(passwordReturn);
        //这里是需要往t_loginfo表中插入数据
        userInfoFeignClient.logininfoAdd(employee);
        //todo 新增成功后发送短信或邮箱 告知用户注册/新增成功
        return AjaxResult.me();
    }

    @GlobalTransactional(rollbackFor = Exception.class)//开启seata全局事务
    @Override
    public  AjaxResult test(Employee employee) {


            //1.先去新增员工
            employeeMapper.insert(employee);
            //2.通过feign调用新增中间表
            userInfoFeignClient.logininfoAdd(employee);
            int a = 1 / 0;

        return null;
    }
}
